﻿using DTCash.Logger;
using DTCash.Logger.DTO;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DTCash.Redis;
using Uranus.Common;
using Uranus.Service.OrderService;
using Uranus.Service.UserService;
using BaseResponse = Uranus.Service.UserService.BaseResponse;

namespace Uranus.Service
{
    public class User : BaseData
    {
        private static User user;
        private User()
        {

        }

        public static User GetUser()
        {
            if (user == null)
            {
                user = new User();
            }
            return user;
        }

        #region 获取用户信息金额等
        /// <summary>
        /// 根据ID获取用户
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public UserDTO GetUserByUserID(int userId)
        {
            UserService.UserServiceClient userServiceClick = new UserServiceClient();
            try
            {
                CacheKey cacheKey = CacheItem.InitByIdentity(userId + "", "GetUserByUserID").User.Info;
                UserDTO userDto = new UserDTO();
                Func<UserDTO> method = delegate
                {
                    UserResponse userRes = userServiceClick.GetUserByUserID(userId);
                    if (userRes != null && userRes.Code == 1 && userRes.UserDto.UserID>0)
                    {
                        userDto = userRes.UserDto;
                    }
                    else
                    {
                        return null;
                    }
                    return userDto;
                };
                return base.GetData<UserDTO>(method, cacheKey);
            }
            catch (Exception ex)
            {
                LogHelper.WriteLog(LogEnum.Error, "Uranus.Service.User.GetUserByUserID 出现异常", ex.Message.ToString(), userId);
                return null;
            }
            finally
            {
                userServiceClick.Close();
            }
        }

        /// <summary>
        /// 根据ID获取用户属性
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public List<UserPropertySubDTO> GetUserPropertySubByUserID(int userId)
        {
            UserService.UserServiceClient userServiceClick = new UserServiceClient();
            try
            {
                CacheKey cacheKey = CacheItem.InitByIdentity(userId + "", "GetUserPropertySubByUserID").User.PropertyValue;
                List<UserPropertySubDTO> userPropertyDtos = new List<UserPropertySubDTO>();
                Func<List<UserPropertySubDTO>> method = delegate
                {
                    UserPropertySubResponse userPropertyRes = userServiceClick.GetUserPropertySubByUserID(userId);
                    if (userPropertyRes != null && userPropertyRes.Code == 1)
                    {
                        userPropertyDtos = userPropertyRes.userPropertySubDtos ?? new List<UserPropertySubDTO>();
                    }
                    else
                    {
                        return null;
                    }
                    return userPropertyDtos;
                };
                return base.GetData<List<UserPropertySubDTO>>(method, cacheKey);
            }
            catch (Exception ex)
            {
                LogHelper.WriteLog(LogEnum.Error, "Uranus.Service.User.GetUserPropertySubByUserID 出现异常", ex.Message.ToString(), userId);
                return null;
            }
            finally
            {
                userServiceClick.Close();
            }
        }

        /// <summary>
        /// 根据ID获取用户
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public UserHomePagePriceResponse GetUserHomePagePriceResponse(int userId)
        {
            UserService.UserServiceClient userServiceClick = new UserServiceClient();
            try
            {
                CacheKey cacheKey = CacheItem.InitByIdentity(userId + "", "GetUserHomePagePriceResponse").User.Price;
                Func<UserHomePagePriceResponse> method = delegate
                {
                    UserHomePagePriceResponse res = userServiceClick.GetUserHomePagePriceResponse(userId);
                    return res.Code == 1 ? res : null;
                };
                return base.GetData<UserHomePagePriceResponse>(method, cacheKey);
            }
            catch (Exception ex)
            {
                LogHelper.WriteLog(LogEnum.Error, "Uranus.Service.User.UserHomePagePriceResponse 出现异常", ex.Message.ToString(), userId);
                return null;
            }
            finally
            {
                userServiceClick.Close();
            }
        }

        /// <summary>
        /// 根据用户名获取用户信息
        /// Author:LHL 2016-10-08 11:
        /// </summary>
        /// <param name="userRequest"></param>
        /// <returns></returns>
        public UserResponse GetUserDTOByMobile(string mobile)
        {
            UserService.UserServiceClient userServiceClick = new UserServiceClient();
            try
            {
                return userServiceClick.GetUserDTOByMobile(mobile); ;
            }
            catch (Exception ex)
            {
                LogHelper.WriteLog(LogEnum.Error, "Uranus.Service.User.GetUserDTOByMobile 出现异常", ex.Message.ToString(), mobile);
                return null;
            }
            finally
            {
                userServiceClick.Close();
            }
        }

        /// <summary>
        /// 根据身份证获取用户信息
        /// </summary>
        /// <param name="userRequest"></param>
        /// <returns></returns>
        public UserDTO GetUserDtoByIDCard(UserRequest userRequest)
        {
            UserService.UserServiceClient userServiceClient = new UserServiceClient();
            try
            {
                CacheKey cacheKey = CacheItem.InitByIdentity(userRequest.UserDto.IDCard + "", "GetUserDtoByIDCard").User.Info;
                Func<UserDTO> method = delegate
                {

                    UserResponse userRes = userServiceClient.GetUserDtoByIDCard(userRequest);
                    if (userRes != null && userRes.UserDto != null)
                    {
                        return userRes.UserDto;
                    }
                    return null;
                };
                return base.GetData<UserDTO>(method, cacheKey);
            }
            catch (Exception ex)
            {
                LogHelper.WriteLog(LogEnum.Error, "Uranus.Service.User.GetUserDtoByIDCard 出现异常", ex.Message.ToString(), userRequest);
                return null;
            }
            finally
            {
                userServiceClient.Close();
            }
        }

        /// <summary>
        /// 根据银行卡号获取用户信息
        /// </summary>
        /// <param name="cardno"></param>
        /// <returns></returns>
        public UserDTO GetUserDtoByCardNo(string cardno)
        {
            UserService.UserServiceClient userServiceClient = new UserServiceClient();
            try
            {
                CacheKey cacheKey = CacheItem.InitByIdentity(cardno + "", "GetUserDtoByCardNo").User.Info;
                Func<UserDTO> method = delegate
                {

                    UserResponse userRes = userServiceClient.GetUserDtoByCardNo(cardno);
                    if (userRes != null && userRes.UserDto != null)
                    {
                        return userRes.UserDto;
                    }
                    return null;
                };
                return base.GetData<UserDTO>(method, cacheKey);
            }
            catch (Exception ex)
            {
                LogHelper.WriteLog(LogEnum.Error, "Uranus.Service.User.GetUserDtoByCardNo 出现异常", ex.Message.ToString(), cardno);
                return null;
            }
            finally
            {
                userServiceClient.Close();
            }
        }
        #endregion



        #region 注册
        /// <summary>
        /// Code = 2 md5加密失败， Code = 3 添加用户表数据失败， Code = 4 该用户已存在， Code = 5 生成用户统计数据失败， Code = 6 接口执行异常
        /// </summary>
        /// <param name="userRequest"></param>
        /// <returns></returns>
        public UserResponse RegisterUser(UserRequest userRequest)
        {
            UserService.UserServiceClient userServiceClick = new UserServiceClient();
            try
            {
                return userServiceClick.RegisterUser(userRequest);
            }
            catch (Exception ex)
            {
                LogHelper.WriteLog(LogEnum.Error, "Uranus.Service.User.RegisterUser 出现异常", ex.Message.ToString(), userRequest);

                return null;
            }
            finally
            {
                userServiceClick.Close();
            }
        }
        #endregion

        #region 登录
        /// <summary>
        /// 用户登录   code = 2 获取用户数据失败  code = 3 用户不存在  code = 4 md5加密失败  code = 5 用户密码错误
        /// </summary>
        /// <param name="userRequest"></param>
        /// <returns></returns>
        public UserResponse UserLogin(UserRequest userRequest)
        {
            UserService.UserServiceClient userServiceClick = new UserServiceClient();
            try
            {
                return userServiceClick.UserLogin(userRequest);
            }
            catch (Exception ex)
            {
                LogHelper.WriteLog(LogEnum.Error, "Uranus.Service.User.UserLogin 出现异常", ex.Message.ToString(), userRequest);

                return null;
            }
            finally
            {
                userServiceClick.Close();
            }
        }
        #endregion

        #region 修改登录密码

        /// <summary>
        /// 根据用户手机号更新用户密码
        /// </summary>
        /// <param name="userRequest"></param>
        /// <returns></returns>
        public UserResponse UpdatePasswordByMobile(UserRequest userRequest)
        {
            UserService.UserServiceClient userServiceClick = new UserServiceClient();
            try
            {
                return userServiceClick.UpdatePasswordByMobile(userRequest);
            }
            catch (Exception ex)
            {
                LogHelper.WriteLog(LogEnum.Error, "Uranus.Service.User.UpdatePasswordByMobile 出现异常", ex.Message.ToString(), userRequest);

                return null;
            }
            finally
            {
                userServiceClick.Close();
            }
        }
        #endregion


        #region 获取用户XX记录

        /// <summary>
        /// 获取用户交易记录
        /// </summary>
        /// <param name="userTransactionRequest"></param>
        /// <returns></returns>
        public UserTransactionResponse GetUserTransactionDTOList(UserTransactionRequest userTransactionRequest)
        {
            UserService.UserServiceClient userServiceClick = new UserServiceClient();
            try
            {
                List<object> keylist = new List<object>();
                keylist.Add("GetUserTransactionDTOList");
                keylist.Add(userTransactionRequest.PageIndex);
                keylist.Add(userTransactionRequest.PageSize);
                if (userTransactionRequest.UserTransferTypeIDs != null && userTransactionRequest.UserTransferTypeIDs.Count > 0)
                {
                    keylist.Add(string.Join(",", userTransactionRequest.UserTransferTypeIDs));
                }
                if (userTransactionRequest.UserTransferPropertyIDs != null && userTransactionRequest.UserTransferPropertyIDs.Count > 0)
                {
                    keylist.Add(string.Join(",", userTransactionRequest.UserTransferPropertyIDs));
                }
                CacheKey cacheKey = CacheItem.InitByIdentity(userTransactionRequest.UserID + "", keylist.ToArray()).User.Info;
                Func<UserTransactionResponse> method = delegate
                {
                    return userServiceClick.GetUserTransactionDTOList(userTransactionRequest);
                };
                return base.GetData<UserTransactionResponse>(method, cacheKey);
            }
            catch (Exception ex)
            {
                LogHelper.WriteLog(LogEnum.Error, "Uranus.Service.User.GetUserTransactionDTOList 出现异常", ex.Message.ToString(), userTransactionRequest);
                return null;
            }
            finally
            {
                userServiceClick.Close();
            }
        }

        /// <summary>
        /// 获取用户所有的交易记录
        /// </summary>
        /// <param name="userTransactionRequest"></param>
        /// <returns></returns>
        public UserTransactionResponse GetUserTransactionAllList(UserTransactionRequest userTransactionRequest)
        {
            UserServiceClient userServiceClick = new UserServiceClient();
            try
            {
                List<object> keylist = new List<object>();
                keylist.Add("GetUserTransactionAllList");
                keylist.Add(userTransactionRequest.PageIndex);
                keylist.Add(userTransactionRequest.PageSize);
                CacheKey cacheKey = CacheItem.InitByIdentity(userTransactionRequest.UserID + "", keylist.ToArray()).User.Info;
                Func<UserTransactionResponse> method = delegate
                {
                    return userServiceClick.GetUserTransactionAllList(userTransactionRequest);
                };
                return base.GetData<UserTransactionResponse>(method, cacheKey);
            }
            catch (Exception ex)
            {
                LogHelper.WriteLog(LogEnum.Error, "Uranus.Service.User.GetUserTransactionAllList 出现异常", ex.Message, userTransactionRequest);
                return null;
            }
            finally
            {
                userServiceClick.Close();
            }
        }

        #endregion

        #region 用户银行相关接口

        /// <summary>
        /// 获取用户可用银行卡数据
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public UserBankCardResponse GetUserBankCardDTOList(int userId)
        {
            UserServiceClient userServiceClick = new UserServiceClient();
            try
            {
                Func<UserBankCardResponse> method = delegate
                    {
                        UserBankCardResponse response = userServiceClick.GetUserBankCardDTOList(userId);
                        return response.Code == 1 ? response : null;
                    };
                return base.GetData<UserBankCardResponse>(method
                    , CacheItem.InitByIdentity(userId.ToString(), "GetUserBankCardDTOList").User.BankList) ??
                    new UserBankCardResponse();
            }
            catch (Exception ex)
            {
                LogHelper.WriteLog(LogEnum.Error, "获取用户可用银行卡数据 出现异常 GetUserBankCardDTOList", ex.Message.ToString(), userId);
                return new UserBankCardResponse();
            }
            finally
            {
                userServiceClick.Close();
            }
        }

        /// <summary>
        /// 获取银行列表
        /// </summary>
        /// <returns></returns>
        public BankListResponse GetBankInfoList()
        {
            UserServiceClient userServiceClick = new UserServiceClient();
            try
            {
                Func<BankListResponse> method = delegate
                {
                    BankListResponse response = userServiceClick.GetBankInfoList();
                    return response.Code == 1 ? response : null;
                };
                return base.GetData<BankListResponse>(method, CacheItem.InitByIdentity("BankInfoList").User.BankList) ??
                       new BankListResponse();
            }
            catch (Exception ex)
            {
                LogHelper.WriteLog(LogEnum.Error, "获取银行列表 出现异常 GetBankInfoList", ex.Message);
                return new BankListResponse();
            }
            finally
            {
                userServiceClick.Close();
            }
        }

        /// <summary>
        /// 查找银行支付单关于银行的信息(BankInfo)  根据机构支付方式查找
        /// </summary>
        /// <returns></returns>
        public BankListResponse GetBankInfoDTOByBankPaymentTypeID(int bankPaymentTypeId)
        {
            UserServiceClient userServiceClick = new UserServiceClient();
            try
            {
                Func<BankListResponse> method = delegate
                {
                    return userServiceClick.GetBankInfoDTOByBankPaymentTypeID(bankPaymentTypeId);
                };
                return base.GetData<BankListResponse>(method, CacheItem.InitByIdentity(bankPaymentTypeId.ToString(), "BankList").Other.Info) ??
                    new BankListResponse();
            }
            catch (Exception ex)
            {
                LogHelper.WriteLog(LogEnum.Error, "获取银行卡列表 出现异常 GetBankInfoDTOByBankPaymentTypeID", ex.Message);
                return new BankListResponse();
            }
            finally
            {
                userServiceClick.Close();
            }
        }

        /// <summary>
        /// 根据用户ID更改用户银行卡状态
        /// </summary>
        /// <param name="userBankCardRequest"></param>
        /// <returns></returns>
        public UserBankCardResponse UpdateUserBankCardStatusByUserID(UserBankCardRequest userBankCardRequest)
        {
            UserServiceClient userServiceClick = new UserServiceClient();
            try
            {
                return userServiceClick.UpdateUserBankCardStatusByUserID(userBankCardRequest);
            }
            catch (Exception ex)
            {
                LogHelper.WriteLog(LogEnum.Error, "根据用户ID更改用户银行卡状态 出现异常 UpdateUserBankCardStatusByUserID",
                    ex.Message.ToString(), new
                    {
                        UserID = userBankCardRequest.UserBankCardDto.UserBankCardID,
                        UserBankCardStatusID = userBankCardRequest.UserBankCardDto.UserBankCardStatusID
                    });
                return new UserBankCardResponse();
            }
            finally
            {
                userServiceClick.Close();
            }
        }

        /// <summary>
        /// 添加用户银行卡
        /// </summary>
        /// <param name="userBankCardRequest"></param>
        /// <returns></returns>
        public UserBankCardResponse InsertUserBankCard(UserBankCardRequest userBankCardRequest)
        {
            UserServiceClient userServiceClick = new UserServiceClient();
            try
            {
                return userServiceClick.InsertUserBankCard(userBankCardRequest);
            }
            catch (Exception ex)
            {
                LogHelper.WriteLog(LogEnum.Error, "添加用户银行卡 出现异常 InsertUserBankCard", ex.Message.ToString(), userBankCardRequest.UserBankCardDto);
                return new UserBankCardResponse();
            }
            finally
            {
                userServiceClick.Close();
            }
        }


        #endregion

        #region 用户交易使用接口

        /// <summary>
        /// 添加用户交易数据
        /// </summary>
        /// <param name="userTransfer"></param>
        /// <returns></returns>
        public BaseResponse InsertUserTransfer(UserTransferRequest userTransfer)
        {
            UserServiceClient userServiceClick = new UserServiceClient();
            try
            {
                return userServiceClick.InsertUserTransfer(userTransfer);
            }
            catch (Exception ex)
            {
                LogHelper.WriteLog(LogEnum.Error, "添加用户交易数据 InsertUserTransfer", ex.Message.ToString(), userTransfer.userTransferDTO);
                return new BaseResponse();
            }
            finally
            {
                userServiceClick.Close();
            }
        }

        /// <summary>
        /// 根据用户交易ID获取用户交易数据
        /// </summary>
        /// <param name="userTransferId"></param>
        /// <returns></returns>
        public UserTransferResponse GetUserTransferDTOByTransferID(int userTransferId)
        {
            UserServiceClient userServiceClick = new UserServiceClient();
            try
            {
                return userServiceClick.GetUserTransferDTOByTransferID(userTransferId);
            }
            catch (Exception ex)
            {
                LogHelper.WriteLog(LogEnum.Error, "根据用户交易ID获取用户交易数据 失败 GetUserTransferDTOByTransferID", ex.Message.ToString(), userTransferId);
                return new UserTransferResponse();
            }
            finally
            {
                userServiceClick.Close();
            }
        }



        #endregion
        
        #region 用户已使用的礼品卡数据获取

        /// <summary>
        /// 获取用户礼品卡数据
        /// </summary>
        /// <param name="userGiftCardRequest"></param>
        /// <returns></returns>
        public UserGiftCardResponse GetUserGiftCardDTOList(UserGiftCardRequest userGiftCardRequest)
        {
            UserServiceClient userServiceClick = new UserServiceClient();
            try
            {
                Func<UserGiftCardResponse> method = delegate
                {
                    UserGiftCardResponse response = userServiceClick.GetUserGiftCardDTOList(userGiftCardRequest);
                    return response.Code == 2 ? null : response;
                };
                return GetData<UserGiftCardResponse>(method,
                           CacheItem.InitByIdentity(userGiftCardRequest.UserID.ToString(), "GetUserGiftCardDTOList"
                               , userGiftCardRequest.OrderItemID
                               , string.Join(",", userGiftCardRequest.GiftCardTypeIDs)).User.GiftCard) ?? new UserGiftCardResponse();

            }
            catch (Exception ex)
            {
                LogHelper.WriteLog(LogEnum.Error, "获取用户礼品卡数据 失败 userGiftCardRequest", ex.Message.ToString(), userGiftCardRequest);
                return new UserGiftCardResponse();
            }
            finally
            {
                userServiceClick.Close();
            }
        }

        /// <summary>
        /// 获取礼品卡类型
        /// </summary>
        /// <returns></returns>
        public GiftCardResponse GetGiftCardType()
        {
            UserServiceClient userServiceClick = new UserServiceClient();
            try
            {
                Func<GiftCardResponse> method = delegate
                {
                    GiftCardResponse response = userServiceClick.GetGiftCardType();
                    return response.Code != 1 ? null : response;
                };
                return GetData<GiftCardResponse>(method,
                    CacheItem.InitByIdentity("GiftCardType").User.GiftCard) ?? new GiftCardResponse();
            }
            catch (Exception ex)
            {
                LogHelper.WriteLog(LogEnum.Error, "获取礼品卡类型 失败 GetGiftCardType", ex.Message.ToString(), null);
                return new GiftCardResponse();
            }
            finally
            {
                userServiceClick.Close();
            }
        }

        #endregion

        #region 用户未使用礼品卡

        /// <summary>
        /// 获取可使用的礼品卡
        /// </summary>
        /// <param name="giftCardRequest"></param>
        /// <returns></returns>
        public GiftCardResponse GetUseForGiftCardDTOList(GiftCardRequest giftCardRequest)
        {
            UserServiceClient userServiceClick = new UserServiceClient();
            try
            {
                Func<GiftCardResponse> method = delegate
                {
                    GiftCardResponse response = userServiceClick.GetUseForGiftCardDTOList(giftCardRequest);
                    return response.Code != 1 ? null : response;
                };
                return GetData<GiftCardResponse>(method, 
                    CacheItem.InitByIdentity(giftCardRequest.UserID.ToString(), "UseForGiftCardDTOList", string.Join(",", giftCardRequest.GiftCardTypeIDs)).User.GiftCard) 
                    ?? new GiftCardResponse();
            }
            catch (Exception ex)
            {
                LogHelper.WriteLog(LogEnum.Error, "获取可使用的礼品卡失败 GetUseForGiftCardDTOList", ex.Message, new
                    {
                        giftCardRequest.UserID,
                        GiftCardTypeIDs = string.Join(",", giftCardRequest.GiftCardTypeIDs)
                    });
                return new GiftCardResponse();
            }
            finally
            {
                userServiceClick.Close();
            }
        }

        /// <summary>
        ///  获取已使用的现金卡，前端API使用API
        /// </summary>
        /// <param name="giftCardRequest"></param>
        /// <returns></returns>
        public GiftCardResponse GetUsedGiftCardDTOListByAPI(GiftCardRequest giftCardRequest)
        {
            UserServiceClient userServiceClick = new UserServiceClient();
            try
            {
                Func<GiftCardResponse> method = delegate
                {
                    GiftCardResponse response = userServiceClick.GetUsedGiftCardDTOListByAPI(giftCardRequest);
                    return response.Code != 1 ? null : response;
                };

               
                return GetData<GiftCardResponse>(method,
                           CacheItem.InitByIdentity(giftCardRequest.UserID.ToString(), "GetUsedGiftCardDTOListByAPI").User.GiftCard)
                       ?? new GiftCardResponse();
            }
            catch (Exception ex)
            {
                LogHelper.WriteLog(LogEnum.Error, "获取已使用的现金卡，前端API使用 GetUsedGiftCardDTOListByAPI", ex.Message, new
                {
                    giftCardRequest.UserID,
                    GiftCardTypeIDs = string.Join(",", giftCardRequest.GiftCardTypeIDs)
                });
                return new GiftCardResponse();
            }
            finally
            {
                userServiceClick.Close();
            }
        }

        /// <summary>
        /// 获取可使用的礼品卡API
        /// </summary>
        /// <param name="giftCardRequest"></param>
        /// <returns></returns>
        public GiftCardResponse GetUseForGiftCardDTOListByAPI(GiftCardRequest giftCardRequest)
        {
            UserServiceClient userServiceClick = new UserServiceClient();
            try
            {
                Func<GiftCardResponse> method = delegate
                {
                    GiftCardResponse response = userServiceClick.GetUseForGiftCardDTOListByAPI(giftCardRequest);
                    return response.Code != 1 ? null : response;
                };

                string GiftCardFromIDs = string.Empty;
                if (giftCardRequest.GiftCardFromIDs!=null)
                {
                    GiftCardFromIDs = string.Join(",", giftCardRequest.GiftCardFromIDs);
                }

                return GetData<GiftCardResponse>(method,
                           CacheItem.InitByIdentity(giftCardRequest.UserID.ToString(), "GetUseForGiftCardDTOListAPI", string.Join(",", giftCardRequest.GiftCardTypeIDs), string.Join(",", giftCardRequest.GiftCardStatusIDs), GiftCardFromIDs).User.GiftCard)
                       ?? new GiftCardResponse();
            }
            catch (Exception ex)
            {
                LogHelper.WriteLog(LogEnum.Error, "获取可使用的礼品卡失败 GetUseForGiftCardDTOListAPI", ex.Message, new
                {
                    giftCardRequest.UserID,
                    GiftCardTypeIDs = string.Join(",", giftCardRequest.GiftCardTypeIDs)
                });
                return new GiftCardResponse();
            }
            finally
            {
                userServiceClick.Close();
            }
        }
        #endregion

        #region 查询用户是否开通汇付
        /// <summary>
        /// 根据用户编号查询是否开通汇付
        /// Date:2017-10-13 15:17:29
        /// Author:Linsan
        /// </summary>
        /// <param name="UserID">用户id</param>
        /// <returns></returns>
        public BaseResponse VerifyUser(int UserID)
        {
            UserServiceClient userServiceClick = new UserServiceClient();
            try
            {
                return userServiceClick.VerifyUser(UserID);
            }
            catch (Exception ex)
            {
                LogHelper.WriteLog(LogEnum.Error, "根据用户编号查询是否开通汇付 失败 VerifyUser", ex.Message, null);
                return new UserResponse();
            }
            finally
            {
                userServiceClick.Close();
            }
        }
        #endregion

        /// <summary>
        /// 更新用户认证信息        Code 1 成功  Code 2 程序执行异常（不可直接用于提示） Code 3 验证失败（可直接用于提示）
        /// Author LHL 2016-10-08
        /// </summary>
        /// <param name="authUserRequest"></param>
        /// <returns></returns>
        public UserResponse AuthUserInfo(AuthUserRequest authUserRequest)
        {
            UserServiceClient userServiceClick = new UserServiceClient();
            try
            {
                return userServiceClick.AuthUserInfo(authUserRequest);
            }
            catch (Exception ex)
            {
                LogHelper.WriteLog(LogEnum.Error, "更新用户认证信息 失败 AuthUserInfo", ex.Message, null);
                return new UserResponse();
            }
            finally
            {
                userServiceClick.Close();
            }
        }

        public UserStatisticsResponse GetUserStatistics(int userId)
        {
            UserServiceClient userServiceClick = new UserServiceClient();
            try
            {
                return userServiceClick.GetUserStatistics(userId);
            }
            catch (Exception ex)
            {
                LogHelper.WriteLog(LogEnum.Error, "获取用户资金信息 失败 GetUserStatistics", ex.Message, null);
                return new UserStatisticsResponse();
            }
            finally
            {
                userServiceClick.Close();
            }
        }
        /// <summary>
        /// 根据用户编号修改用户登录手机
        /// Date:2017-10-20 00:00:00
        /// Author:Linsan
        /// </summary>
        /// <param name="userRequest"></param>
        /// <returns></returns>
        public UserResponse UpdateMobileByUserID(UserRequest userRequest)
        {
            UserServiceClient userServiceClick = new UserServiceClient();
            try
            {
                return userServiceClick.UpdateMobileByUserID(userRequest);
            }
            catch (Exception ex)
            {
                LogHelper.WriteLog(LogEnum.Error, "根据用户编号修改用户登录手机 失败 UpdateMobileByUserID", ex.Message, null);
                return new UserResponse();
            }
            finally
            {
                userServiceClick.Close();
            }
        }

        /// <summary>
        /// 更改用户头像
        /// </summary>
        /// <param name="userRequest"></param>
        /// <returns></returns>
        public UserResponse UpdateUserPhoto(UserRequest userRequest)
        {
            UserServiceClient userServiceClick = new UserServiceClient();
            try
            {
                return userServiceClick.UpdateUserPhoto(userRequest);
            }
            catch (Exception ex)
            {
                LogHelper.WriteLog(LogEnum.Error, "更改用户头像 失败 UpdateUserPhoto", ex.Message, null);
                return new UserResponse();
            }
            finally
            {
                userServiceClick.Close();
            }
        }

        #region 用户消息

            /// <summary>
            /// 根据条件查询用户消息记录
            /// Date:2017-10-23 11:33:26
            /// Author:Linsan
            /// </summary>
            /// <param name="request"></param>
            /// <returns></returns>
        public UserMessageAllResponse GetUserMessageAllByCondition(UserMessageAllRequest request)
        {
            UserServiceClient userServiceClick = new UserServiceClient();
            try
            {
                Func<UserMessageAllResponse> method = delegate
                {
                    UserMessageAllResponse response = userServiceClick.GetUserMessageAllByCondition(request);
                    return response.Code != 1 ? null : response;
                };
                return GetData<UserMessageAllResponse>(method,
                    CacheItem.InitByIdentity(request.UserID.ToString(), "UserMessageAll", request.UserMessageTypeID, request.PageStart, request.PageEnd).User.Message) ?? new UserMessageAllResponse();
            }
            catch (Exception ex)
            {
                LogHelper.WriteLog(LogEnum.Error, "根据条件查询用户消息记录 失败 GetUserMessageAllByCondition", ex.Message.ToString(), null);
                return new UserMessageAllResponse();
            }
            finally
            {
                userServiceClick.Close();
            }
        }

        /// <summary>
        /// 批量修改消息状态
        /// Date:2017-10-23 00:00:00
        /// Author:Linsan
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public UserResponse UpdateUserMessageByCondition(UserMessageEditStatusRequest request)
        {
            UserServiceClient userServiceClick = new UserServiceClient();
            try
            {
                return userServiceClick.UpdateUserMessageByCondition(request);
            }
            catch (Exception ex)
            {
                LogHelper.WriteLog(LogEnum.Error, "批量修改消息状态 失败 UpdateUserMessageByCondition", ex.Message, null);
                return new UserResponse();
            }
            finally
            {
                userServiceClick.Close();
            }
        }

        /// <summary>
        /// 获取用户是否有未读消息
        /// Date:2017-10-23 00:00:00
        /// Author:Linsan
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public BaseResponse GetUserMessageAllStatusIDByUserID(UserMessageAllRequest request)
        {
            UserServiceClient userServiceClick = new UserServiceClient();
            try
            {
                Func<BaseResponse> method = delegate
                {
                    BaseResponse response = userServiceClick.GetUserMessageAllStatusIDByUserID(request);
                    return response.Code != 1 ? null : response;
                };
                return GetData<BaseResponse>(method,
                    CacheItem.InitByIdentity(request.UserID.ToString(), "UserMessageStatus", request.UserMessageTypeID).User.Message) ?? new BaseResponse();
            }
            catch (Exception ex)
            {
                LogHelper.WriteLog(LogEnum.Error, "获取用户是否有未读消息 失败 GetUserMessageAllStatusIDByUserID", ex.Message, null);
                return new UserMessageAllResponse();
            }
            finally
            {
                userServiceClick.Close();
            }
        }

        /// <summary>
        /// 根据编号查询用户消息
        /// Date:2017-10-23 00:00:00
        /// Author:Linsan
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public UserMessageAllResponse GetUserMessageByUserMessageID(UserMessageAllRequest request)
        {
            UserServiceClient userServiceClick = new UserServiceClient();
            try
            {
                Func<UserMessageAllResponse> method = delegate
                {
                    UserMessageAllResponse response = userServiceClick.GetUserMessageByUserMessageID(request);
                    return response.Code != 1 ? null : response;
                };
                return GetData<UserMessageAllResponse>(method,
                    CacheItem.InitByIdentity(request.UserMessageID.ToString(), "UserMessage").User.Message) ?? new UserMessageAllResponse();
            }
            catch (Exception ex)
            {
                LogHelper.WriteLog(LogEnum.Error, "获取用户是否有未读消息 失败 GetUserMessageByUserMessageID", ex.Message, null);
                return new UserMessageAllResponse();
            }
            finally
            {
                userServiceClick.Close();
            }
        }

        /// <summary>
        /// 根据条件查询公告信息
        /// Date:2017-10-24 11:33:26
        /// Author:Linsan
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public NewsAllResponse GetNewsAllByCondition(NewsAllRequest request)
        {
            UserServiceClient userServiceClick = new UserServiceClient();
            try
            {
                Func<NewsAllResponse> method = delegate
                {
                    NewsAllResponse response = userServiceClick.GetNewsAllByCondition(request);
                    return response.Code != 1 ? null : response;
                };
                return GetData<NewsAllResponse>(method,
                    CacheItem.InitByIdentity(request.NewsTypeID.ToString(), "GetNewsAllByCondition", request.PageStart, request.PageEnd).News.Info) ?? new NewsAllResponse();
            }
            catch (Exception ex)
            {
                LogHelper.WriteLog(LogEnum.Error, "根据条件查询公告信息 失败 GetNewsAllByCondition", ex.Message.ToString(), null);
                return new NewsAllResponse();
            }
            finally
            {
                userServiceClick.Close();
            }
        }

        /// <summary>
        /// 根据编号查询公告信息
        /// Date:2017-10-24 11:33:26
        /// Author:Linsan
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public NewsAllResponse GetNewsByUserNewsID(NewsAllRequest request)
        {
            UserServiceClient userServiceClick = new UserServiceClient();
            try
            {
                Func<NewsAllResponse> method = delegate
                {
                    NewsAllResponse response = userServiceClick.GetNewsByUserNewsID(request);
                    return response.Code != 1 ? null : response;
                };
                return GetData<NewsAllResponse>(method,
                    CacheItem.InitByIdentity(request.NewsID.ToString(), "NewsInfo").News.Info) ?? new NewsAllResponse();
            }
            catch (Exception ex)
            {
                LogHelper.WriteLog(LogEnum.Error, "根据编号查询公告信息 失败 GetNewsByUserNewsID", ex.Message.ToString(), null);
                return new NewsAllResponse();
            }
            finally
            {
                userServiceClick.Close();
            }
        }
        #endregion

    

        /// <summary>
        /// 根据huifucode返回user
        /// </summary>
        /// <param name="userDTORequest"></param>
        /// <returns></returns>
        public UserResponse GetUserDtoByHuiFuCode(string HuiFuCode)
        {
            UserServiceClient userServiceClick = new UserServiceClient();
            try
            {
                return userServiceClick.GetUserDtoByHuiFuCode(HuiFuCode);
            }
            catch (Exception ex)
            {
                LogHelper.WriteLog(LogEnum.Error, "根据huifucode返回user 失败 GetUserDtoByHuiFuCode", ex.Message.ToString(), null);
                return new UserResponse();
            }
            finally
            {
                userServiceClick.Close();
            }
        }

        #region 礼品卡接口

        /// <summary>
        /// 获取礼品卡集合
        /// </summary>
        /// <param name="giftCardRequest"></param>
        /// <returns></returns>
        public GiftCardResponse GetGiftCardDTOList(GiftCardRequest giftCardRequest)
        {
            UserServiceClient userServiceClick = new UserServiceClient();
            try
            {
                giftCardRequest.GiftCardTypeIDs = giftCardRequest.GiftCardTypeIDs ?? new List<int>();
                giftCardRequest.GiftCardStatusIDs = giftCardRequest.GiftCardStatusIDs ?? new List<int>();

                Func<GiftCardResponse> method = () =>
                    {
                        GiftCardResponse response = userServiceClick.GetGiftCardDTOList(giftCardRequest);
                        return response.Code != 1 ? null : response;
                    };
                return GetData<GiftCardResponse>(method, CacheItem.InitByIdentity(giftCardRequest.UserID.ToString(),
                                                                          "GetGiftCardDTOList",
                                                                          giftCardRequest.GiftCardID,
                                                                          string.Join(",", giftCardRequest.GiftCardTypeIDs), string.Join(",", giftCardRequest.GiftCardStatusIDs),
                                                                          //giftCardRequest.GiftCardFromID,
                                                                          giftCardRequest.PageIndex,
                                                                          giftCardRequest.PageSize).User.GiftCard) 
                                                                          ?? new GiftCardResponse();
            }
            catch (Exception ex)
            {
                string content = string.Format("params:{0}", CodeCommon.Serialize(giftCardRequest));
                LogHelper.WriteLogError("获取礼品卡集合异常 GetGiftCardDTOList", content, ex);
                return new GiftCardResponse();
            }
            finally
            {
                userServiceClick.Close();
            }
        }

        /// <summary>
        /// 加息卡页面的收益
        /// </summary>
        /// <param name="giftCardRequest"></param>
        /// <returns></returns>
        public BaseResponse GetGiftCardSumprofit(GiftCardRequest giftCardRequest)
        {
            UserServiceClient userServiceClick = new UserServiceClient();
            try
            { 
                //return userServiceClick.GetGiftCardSumprofit(giftCardRequest);

                Func<BaseResponse> method = delegate
                {
                    BaseResponse response = userServiceClick.GetGiftCardSumprofit(giftCardRequest);
                    return response.Code != 1 ? null : response;
                };
                return GetData<BaseResponse>(method,
                           CacheItem.InitByIdentity(giftCardRequest.UserID.ToString(), "GiftCardSumprofit").User.Price) ?? new BaseResponse();
                /*和金额相关的一律用Price，逗号分隔*/
            }
            catch (Exception ex)
            {
                LogHelper.WriteLog(LogEnum.Error, "加息卡页面的收益 GetGiftCardSumprofit", ex.Message, null);
                return new BaseResponse();
            }
            finally
            {
                userServiceClick.Close();
            }
        }

        /// <summary>
        /// 投资使用礼品卡
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="giftCardId"></param>
        /// <param name="orderItemId"></param>
        /// <returns></returns>
        public UserGiftCardResponse InvestUseGiftCard(int giftCardId, int orderItemId)
        {
            UserServiceClient userServiceClick = new UserServiceClient();
            try
            {
                return userServiceClick.InvestUseGiftCard(giftCardId, orderItemId);
            }
            catch (Exception ex)
            {
                LogHelper.WriteLog(LogEnum.Error, "投资使用礼品卡 InvestUseGiftCard", ex.Message, null);
                return new UserGiftCardResponse();
            }
            finally
            {
                userServiceClick.Close();
            }
        }

        #endregion

        /// <summary>
        /// 获取用户礼品卡数据 多个条件拼接
        /// </summary>
        /// <returns></returns>
        public UserGiftCardResponse GetUserGiftCardDTOByParams(UserGiftCardRequest userGiftCardRequest)
        {
            UserServiceClient userServiceClick = new UserServiceClient();
            try
            {
                Func<UserGiftCardResponse> method = delegate
                {
                    UserGiftCardResponse response = userServiceClick.GetUserGiftCardDTOByParams(userGiftCardRequest);
                    return response.Code == 2 ? null : response;
                };
                return GetData<UserGiftCardResponse>(method,
                           CacheItem.InitByIdentity(userGiftCardRequest.UserID.ToString(),"UserGiftCard", userGiftCardRequest.UserGiftCardStatusID).User.GiftCard) ?? new UserGiftCardResponse();
            }
            catch (Exception ex)
            {
                LogHelper.WriteLog(LogEnum.Error, "获取用户礼品卡数据 GetUserGiftCardDTOByParams", ex.Message, null);
                return new UserGiftCardResponse();
            }
            finally
            {
                userServiceClick.Close();
            }
        }

        /// <summary>
        /// 更改用户余额（在原先的余额上相加）
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="price"></param>
        /// <returns></returns>
        public int UpdUsablePrice(int userId, decimal price)
        {
            UserServiceClient userServiceClick = new UserServiceClient();
            try
            {
                return userServiceClick.UpdUsablePrice(userId, price);
            }
            catch (Exception ex)
            {
                LogHelper.WriteLog(LogEnum.Error, "更改用户余额（在原先的余额上相加） UpdUsablePrice", ex.Message, null);
                return 0;
            }
            finally
            {
                userServiceClick.Close();
            }
        }

        /// <summary>
        /// 发送站内信
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="title"></param>
        /// <param name="content"></param>
        /// <param name="noticeLevelEnum"></param>
        public void SendUserMessage(int userId, string title, string content,
            NoticeLevelEnum noticeLevelEnum = NoticeLevelEnum.General)
        {
            UserServiceClient userServiceClick = new UserServiceClient();
            try
            {
                userServiceClick.SendUserMessage(userId, title, content, noticeLevelEnum);
            }
            catch (Exception ex)
            {
                LogHelper.WriteLog(LogEnum.Error, "发送站内信 SendUserMessage", ex.Message,
                    new {userId, title, content, noticeLevelEnum}, true);
            }
            finally
            {
                userServiceClick.Close();
            }
        }

        /// <summary>
        /// 发送短信入队
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="content"></param>
        /// <param name="noticeLevelEnum"></param>
        public void SendNote(int userId, string content,
            NoticeLevelEnum noticeLevelEnum = NoticeLevelEnum.General)
        {
            UserServiceClient userServiceClick = new UserServiceClient();
            try
            {
                userServiceClick.SendNote(userId, content, noticeLevelEnum);
            }
            catch (Exception ex)
            {
                LogHelper.WriteLog(LogEnum.Error, "发送短信入队 SendNote", ex.Message,
                    new { userId, content, noticeLevelEnum }, true);
            }
            finally
            {
                userServiceClick.Close();
            }
        }
    }
}